#!/bin/bash
##一键配置docker服务
echo -e "The IS $0"
chek_docker(){
	sudo systemctl restart docker
	if [[ "$?" == '0' ]];then
		echo 'docker is runing'
		docker info 
	else
		echo -e "Docker启动失败\n是否重置docker配置[y/n]\n默认Y"
		read inits
		if [[ "$inits" == "n" || "$inits" == "N" ]];then
			echo '建议查看/etc/docker/daemon.json配置'
		else
			echo -e "{\n\t\"registry-mirrors\": [\"http://hub-mirror.c.163.com\"]\n}" > /etc/docker/daemon.json
		fi
	fi
}
load_images(){
	echo -e "input File Path"
	read p
	pushd $p
	while [ -z "$i" ];do
		if [[ -z `ls | grep .tar` ]];then
			echo '此目录为空'
			load_images
		else
			for im in `ls *.tar`;do
				sudo docker load -i $im
			done
		fi
		echo '回车继续,输入任意字符退出'
		read i
	done
}
echo -e "Please select mune
[1]config Registry
[2]config Cgroup Driver
[3]init docker
[4]load images For tar File"
read cs
if [[ "$cs" == '1' ]];then
	echo -e "Please Registry\n[1]aliyun\n[2]163\n[3]USTC\n[default]2+3"
	read rs
	if [[ $rs == '1' ]];then
		echo -e "{\n   \"registry-mirrors\": [\"https://r61ch9pn.mirror.aliyuncs.com\"]\n}" > /etc/docker/daemon.json
	elif [[ $rs == '2' ]];then
		echo -e "{\n\t\"registry-mirrors\": [\"http://hub-mirror.c.163.com\"]\n}" > /etc/docker/daemon.json
	elif [[ $rs == '3' ]];then
		echo -e "{\n\t\"registry-mirrors\": [\"https://docker.mirrors.ustc.edu.cn\"]\n}" > /etc/docker/daemon.json
	else
		echo -e "{\n   \"registry-mirrors\": [\"http://hub-mirror.c.163.com\",\"https://docker.mirrors.ustc.edu.cn\"]\n}" > /etc/docker/daemon.json
	fi
	chek_docker
elif [[ "$cs" == '2' ]];then
	echo -e "[1]set to system\n[2]set to Cgroup Driver"
	read sets
	if [[ "$sets" == '1' ]];then
		#sed -i 's/}//g' /etc/docker/daemon.json
		#sed -i 's/\]/\],/g' /etc/docker/daemon.json
		#echo -e "\n\t\"exec-opts\": [\"native.cgroupdriver=system\"]\n}" >> /etc/docker/daemon.json
		#chek_docker
		echo '暂不提供'
	elif [[ "$sets" == '2' ]];then
		echo 'not set'
	else
		echo 'select errors'
	fi
	sudo systemctl daemon-reload
	sudo systemctl restart docker
	if [[ $? == '0' ]];then
		echo 'services is runing'
	else
		echo 'services up failed'
	fi
	docker info
	echo '配置完成'
elif [[ "$cs" == "3" ]];then
	echo -e "{\n\t\"registry-mirrors\": [\"http://hub-mirror.c.163.com\"]\n}" > /etc/docker/daemon.json
	if [ -z `sudo systemctl restart docker` ];then
		docker info
		echo '配置完成'
	else
		echo 'docker启动失败'
	fi
elif [[ "$use" == "4" ]];then
	load_images
else
	echo 'select errors'
fi
